package com.qs.rest.services;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.HttpHeaders;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.qs.entities.Channel;
import com.qs.entities.Feed;
import com.qs.exceptions.ServiceException;
import com.qs.rest.IPublicRestService;
import com.qs.services.ChannelService;
import com.qs.services.FeedService;
import com.qs.utils.HttpStatusCodes;
import com.qs.utils.HttpStatusStrings;
import com.qs.valuesobjects.ChannelVo;
import com.qs.valuesobjects.FeedVo;
import com.qs.valuesobjects.UserVo;
import com.qs.valuesobjects.containers.BaseContainerVo;
import com.qs.valuesobjects.containers.ChannelListContainerVo;
import com.qs.valuesobjects.containers.FeedListContainerVo;

public class PublicRestServiceImpl implements IPublicRestService{

	protected final Logger log = Logger.getLogger(PublicRestServiceImpl.class);
	
	private FeedService feedService;
	private ChannelService channelService;
	
	
	@Override
	public BaseContainerVo getFeeds(HttpHeaders httpHeaders, String offset, String limit,String channelId) {
		log.debug("fetching feeds");
		FeedListContainerVo feedListContainer = new FeedListContainerVo();
		try{
			List<Feed> feeds =  feedService.getFeeds(Integer.parseInt(offset), Integer.parseInt(limit),channelId);
			feedListContainer.meta.code = HttpStatusCodes.OK;
			feedListContainer.data = new ArrayList<FeedVo>();
			
			for(Feed feed : feeds){
				FeedVo feedVo = new FeedVo();
				
				feedVo.text 		= feed.text;
				feedVo.createdOn	= feed.createdOn.toString();
				feedVo.feedId		= feed.feedId;
				
				feedVo.user			= new UserVo();
				feedVo.user.userId 				= feed.user.userId;
				feedVo.user.firstName			= feed.user.firstName;
				feedVo.user.lastName			= feed.user.lastName;
				feedVo.user.screenName			= feed.user.screenName;
				feedVo.user.email				= feed.user.email;
				feedVo.user.address				= feed.user.address;
				feedVo.user.phone				= feed.user.phone;
				feedVo.user.createdOn			= feed.user.createdOn.toString();
				
				feedVo.channel					= new ChannelVo();
				feedVo.channel.code				= feed.channel.code;
				feedVo.channel.name				= feed.channel.name;
				feedVo.channel.orderIndex		= feed.channel.orderIndex.toString();
				
				
				feedListContainer.data.add(feedVo);
				
			}
			
			return feedListContainer;
			
		}
		catch(ServiceException exc){
			exc.printStackTrace(System.err);
			feedListContainer.meta.code 		= HttpStatusCodes.INVALID_REQUEST;
			feedListContainer.meta.error 		= HttpStatusStrings.INVALID_REQUEST;
			feedListContainer.meta.details	= ExceptionUtils.getRootCauseMessage(exc).toString();
			return feedListContainer;
		}
		catch(Exception exc){
			exc.printStackTrace(System.err);
			feedListContainer.meta.code 		= HttpStatusCodes.INTERNAL_SERVER_ERROR;
			feedListContainer.meta.error 		= HttpStatusStrings.INTERNAL_SERVER_ERROR;
			feedListContainer.meta.details	= ExceptionUtils.getRootCauseMessage(exc).toString();
			return feedListContainer;
		}	
	}

	
	@Override
	public BaseContainerVo getAllChannels(HttpHeaders httpHeaders) {
		log.debug("fetching channels");
		ChannelListContainerVo channelListContainer = new ChannelListContainerVo();
		try{
			List<Channel> channels =  channelService.getAllChannels();
			
			channelListContainer.meta.code = HttpStatusCodes.OK;
			channelListContainer.data = new ArrayList<ChannelVo>();
			
			for(Channel channel : channels){
				ChannelVo channelVo = new ChannelVo();
				
				channelVo.code = channel.code;
				channelVo.name = channel.name;
				channelVo.orderIndex = channel.orderIndex.toString();
				
				channelListContainer.data.add(channelVo);
				
			}
			
			return channelListContainer;
			
		}
		catch(ServiceException exc){
			exc.printStackTrace(System.err);
			channelListContainer.meta.code 		= HttpStatusCodes.INVALID_REQUEST;
			channelListContainer.meta.error 		= HttpStatusStrings.INVALID_REQUEST;
			channelListContainer.meta.details	= ExceptionUtils.getRootCauseMessage(exc).toString();
			return channelListContainer;
		}
		catch(Exception exc){
			exc.printStackTrace(System.err);
			channelListContainer.meta.code 		= HttpStatusCodes.INTERNAL_SERVER_ERROR;
			channelListContainer.meta.error 		= HttpStatusStrings.INTERNAL_SERVER_ERROR;
			channelListContainer.meta.details	= ExceptionUtils.getRootCauseMessage(exc).toString();
			return channelListContainer;
		}	

	}
	

	public FeedService getFeedService() {
		return feedService;
	}


	public void setFeedService(FeedService feedService) {
		this.feedService = feedService;
	}


	public ChannelService getChannelService() {
		return channelService;
	}


	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}
	
}
